<!DOCTYPE html>
<html>
<head>
<head/>
<div th:include="base-libs :: base-libs" />
<title>医疗机构</title>
<style type="text/css">

</style>
</head>
<script type="text/javascript">
var selection = [];
var ZDYLJGID;
$(function() {
	$(this).registerManagerPage();
 	initView();
});

function queryForm(status) {
	$("#yljgForm").datagrid("reload");
	if(status) initXzTable();
}

function initView()
{
	var cahce = StoreCache.removeCache("QYSQ_YLJG");
	/**加载值域*/
	//$(".WSJGLBDM").CacheForSelect();
	var jgxx = cahce["jgxx"];
	var url = cahce["url"];
	ZDYLJGID = cahce["ZDYLJGID"];
	selection = jgxx;
	if(url){
		/**给queryForm赋值action*/
		$("#queryForm").attr("action",url);
		if(jgxx){
			var yljgids = "";
			$(jgxx).each(function(index, item){
				yljgids += item["PTJGID"]+",";
			});
			$("#yljgids").val(yljgids);
			initXzTable();
		}
		$("#dataGridDiv").append($("#ks").html());
	} else{
		$("#dataGridDiv").append($("#jg").html());
	}
	initDatagrid(url);
	/**确认按钮*/
	$("#enter").on("click", function(){
		var res = [];
		if(url){
			/**专科联盟生成数据*/
			var rows = $("#xzjgTable").treegrid("getSelections");
			res = getResult(rows);
	 	}else{
			res = $("#yljgForm").datagrid("getSelections");
		}
		StoreCache.setCache("QYSQ_XZYLJG", res);
		parent.layer.closeAll("iframe");
	});
	/**关闭按钮*/
	$("#close").on("click", function(){
		parent.layer.closeAll("iframe");
	});
}

function initDatagrid(url){
	var opt;
	/**加载选中机构、科室信息*/
	if(url){
		opt = {
				idField : "PTJGID",
				uniqueId : "PTJGID",
				striped : true,
				toolbar : "#dgFormToolbarJg",
				columns : [ [ 
				{
					field : "cz",
					width : 80,
					align : "center",
					title : "操作",
					formatter : function(value, row, index){
						value = $("#yljgDetail2").html();
						return value.replace(/#yljgid#/g,row["PTJGID"]);
					}
				},{
					field : "YLJGMC",
					width : 180,
					align : "center",
					title : "医疗机构名称"
				}, {
					field : "XZQHMC",
					width : 150,
					align : "center",
					title : "行政区划"
				}, {
					field : "WSJGLBMC",
					width : 120,
					align : "center",
					title : "机构类型"
				}, {
					field : "YYDJMC",
					width : 100,
					align : "center",
					title : "医院等级"
				}, {
					field : "JGFLGLMC",
					width : 180,
					align : "center",
					title : "经营性质"
				} ] ],
				pagination : true,
				pageNumber : 1,
				pageSize : 10,
				fit : true,
				fitColumns : true,
				singleSelect : true,
				rownumbers : true,
				method : "post",
				url : contextRoot + "yljgxx/queryPage.service",
				onBeforeLoad : function(params)//加载之前回调 获取查询条件
				{
					params["YLJGMC"] = $(".yljgmc").val();
					params["YLJGIDS"] = $("#yljgids").val();
					params["WSJGLBDM"] = $(".WSJGLBDM").val();
					return params;
				},onLoadSuccess: function(data){ 
					/**选中已选行*/
					/* if(!StringUtils.isEmpty(selection)){
						$(selection).each(function(index, item){
							$("#yljgForm").datagrid("selectRecord",item["PTJGID"]);
						});
					} */
				}
			};
		$("#xzjgTable").treegrid({
			idField : "KSDMPK",
			treeField : "KSMC",
			striped : true,
			fit : true,
			fitColumns : true,
			singleSelect : false,
				columns : [ [ 
				{
					field: "checkStatus",
					checkbox: true
				},
				{
					field : "KSMC",
					width : 150,
					title : "请选择"
				}, {
					field : "cz",
					width : 50,
					align : "center",
					title : "操作",
					formatter : function(value, row, index){
						if(row["PTJGID"] == row["KSDM"] && row["PTJGID"] != ZDYLJGID){
							value = $("#tableToolBar").html();
							return value.replace(/#yljgid#/g,row["PTJGID"]);
						}
					}
				}] ],onLoadSuccess: function(row, data){ 
					/**选中已选行*/
					if(!StringUtils.isEmpty(selection)){
						$(selection).each(function(index, item){
							var ksdms = item["KSDM"];
							if(ksdms != "null"){
								ksdms = ksdms.split(",");
								for(var i in ksdms){
									var ksdm = ksdms[i]; 
									if(!StringUtils.isEmpty(ksdm)){
										$("#xzjgTable").treegrid("select",ksdm+":"+item["PTJGID"]);
									}
								}
								$("#xzjgTable").treegrid("select", item["PTJGID"]);
							}else{
								$("#xzjgTable").treegrid("select",item["PTJGID"]);
							}
						});
						/**关闭除最后一个的节点*/
						var yljgids = $("#yljgids").val();
						yljgids = (yljgids.substr(0,yljgids.length-1)).split(",");
						$(yljgids).each(function(index, item){
							if(index < yljgids.length-1){
								$("#xzjgTable").treegrid("collapse",item);
							}
						});
				}
			},onBeforeExpand: function(row){
				$("#xzjgTable").treegrid("collapseAll");
			},onClickRow: function(row){
				if(row){
					$("#xzjgTable").treegrid("select",row["PTJGID"]);
					$("#xzjgTable").treegrid("select",row["KSDMPK"]);	
				}
				return false;
			}
		});
	}else{
		opt = {
				idField : "PTJGID",
				uniqueId : "PTJGID",
				striped : true,
				toolbar : "#dgFormToolbarJg",
				columns : [ [ 
				{
					field: "checkStatus",
					checkbox: true
				},{
					field : "YLJGMC",
					width : 180,
					align : "center",
					title : "医疗机构名称"
				}, {
					field : "XZQHMC",
					width : 150,
					align : "center",
					title : "行政区划"
				}, {
					field : "WSJGLBMC",
					width : 150,
					align : "center",
					title : "机构类型"
				}, {
					field : "YYDJMC",
					width : 150,
					align : "center",
					title : "医院等级"
				}, {
					field : "JGFLGLMC",
					width : 180,
					align : "center",
					title : "经营性质"
				}, {
					field : "cz",
					width : 100,
					align : "center",
					title : "操作",
					formatter : function(value, row, index){
						value = $("#yljgDetail1").html();
						return value.replace(/#yljgid#/g,row["PTJGID"]);
					}
				} ] ],
				pagination : true,
				pageNumber : 1,
				pageSize : 10,
				fit : true,
				fitColumns : true,
				singleSelect : false,
				rownumbers : true,
				method : "post",
				url : contextRoot + "yljgxx/queryPage.service",
				onBeforeLoad : function(params)//加载之前回调 获取查询条件
				{
					params["YLJGMC"] = $(".yljgmc").val();
					params["YLJGIDS"] = $("#yljgids").val();
					params["WSJGLBDM"] = $(".WSJGLBDM").val();
					return params;
				},onLoadSuccess: function(data){ 
					/**选中已选行*/
					if(!StringUtils.isEmpty(selection)){
						$(selection).each(function(index, item){
							$("#yljgForm").datagrid("selectRecord",item["PTJGID"]);
						});
					}
				}
			};
	}
	/**加载机构*/
	$("#yljgForm").datagrid(opt);
}

/**查询按钮点击回调*/
function btnQueryYljgHandler() {
	queryYljgForm();
}

/**机构详情*/
function yljgDetail(yljgid){
	window.open("yljgxq.html?YLJGID="+ yljgid);
}

/**查询按钮*/
function btnQueryYljgHandler(){
	queryForm();
}

/**选中医疗机构*/
function select(yljgid){
	/**删除当前机构*/
	var rows = $("#yljgForm").datagrid("getRows");
	var index = $("#yljgForm").datagrid("getRowIndex",yljgid);
	var row = rows[index];
	$("#yljgForm").datagrid("deleteRow",index);
	$("#yljgids").val($("#yljgids").val() + yljgid + ",");
	//console.log(yljgid);
	queryForm(true);
	//initXzTable();
}

function remove(yljgid){
	var value = $("#yljgids").val();
	var ary = value.split(",");
	$(ary).each(function(index,item){
		if(yljgid == item){
			ary.splice(index, 1);
		}
	});
	$("#yljgids").val(ary.join(","));
	if(!StringUtils.isEmpty(selection)){
		$(selection).each(function(index, item){
			if(item["PTJGID"] == yljgid){
				selection.splice(index, 1);
			}
		});
	}
	$("#xzjgTable").treegrid("remove", yljgid);
	$("#xzjgTable").treegrid("clearSelections");
	queryForm();
	
	//initXzTable();
}

/**生成选中table*/
function initXzTable(){
	var yljgids = $("#yljgids").val();
	if(!StringUtils.isEmpty(yljgids)){
		yljgids = yljgids.substr(0,yljgids.length-1);
	}else{
		yljgids = "";
	}
	$.QuickRemote.AjaxFiledFormSubmit("queryForm", function(ary) { 
		$("#xzjgTable").treegrid("loadData", []);
		if (ary.length > 0) {
			$("#xzjgTable").treegrid("loadData", ary);
		} 
	}, {YLJGIDS : yljgids});
}

/**封装结果*/
function getResult(ary) {
	var res = [];
	/**获取机构id*/
	for(var i in ary){
		var item = ary[i];
		if(typeof(item)=="object" && isExit(res, item)){
			res.push(item);
		}
	}
	/**获取每个机构的科室信息*/
	for(var i in res){
		var ksxx = getKsxx(res[i]["PTJGID"],ary);
		res[i]["KSDM"] = ksxx["KSDM"];
		res[i]["KSMC"] = ksxx["KSMC"];
	}
	return res;	
}

/**判断是否已经存在*/
function isExit(res, item){
	var flag = true;
	for(var i in res){
		if(res[i]["PTJGID"] == item["PTJGID"]){
			flag = false;
		}
	}
	return flag;
}

function getKsxx(ptjgid, ary){
	var ksxx = {};
	var ksdms ="";
	var ksmcs ="";
	for(var i in ary){
		if(ary[i]["PTJGID"] == ptjgid && ary[i]["KSDM"] != ptjgid){
			ksmcs += ary[i]["KSMC"]+",";
			ksdms += ary[i]["KSDM"]+",";
		}
	}
	if(StringUtils.isEmpty(ksdms)){
		ksdms = "null";
	}else{
		ksdms = ksdms.substr(0, ksdms.length-1);
		ksmcs = ksmcs.substr(0, ksmcs.length-1);
	}
	ksxx["KSDM"] = ksdms;
	ksxx["KSMC"] = ksmcs;
	return ksxx;
}
</script>

<body>
<div class="easyui-layout" fit="true" style="width: 100%;">
	<div region="north" style="border: none; overflow: hidden;">
	<!-- <h4 class="h4-title" style="font-size: 18px;">
		医疗机构信息列表
		
	</h4> -->
	</div>
	<div region="center" style="border: none;overflow: hidden;">
	<form id="queryForm"></form>
	<div id="dgFormToolbar" style="background: white;">
		<table class="form-table" style="">
			<tr>
				<td class="td-text">机构名称：</td>
				<td class="td-content">
					<input class="yljgmc" type="text"
						placeholder="机构名称">
					<input id="yljgids" style="display: none;">
				</td>
				<td class="td-text">机构类型：</td>
				<td class="td-content">
					<select class="form-control" cacheTable="TB_ZY_JGLB">
					</select>
				</td>
				<td class="td-text">
					<button class="btn btn-default" type="button"
					onclick="btnQueryYljgHandler()">查询</button>
				</td>
			</tr>
		</table>
	</div>
	<div style="height:330px;">
		<div class="easyui-layout" style="width:100%;border: none;height: 330px;" fit="true" id='dataGridDiv'>
			<!-- <div region="center" style="border: none;">
				<table id="yljgForm">
				</table> 
			</div> -->
		</div>
	</div>
	</div>
	<div region="south" style="border: none;height:40px;">
		<div style="float:right;margin-right: 10px;">
			<button type="button" id="enter">确认</button>
			&nbsp;
			<button type="button" id="close">关闭</button>
		</div>
	</div>
</div>

</body>
<script type="text/html" id="yljgDetail1">
<a href="javascript:yljgDetail('#yljgid#')" title="详细" >详细</a>
</script>
<script type="text/html" id="yljgDetail2">
<a href="javascript:yljgDetail('#yljgid#')" title="详细" >详细</a>
<a class="tableToolBarBtn" href="javascript:select('#yljgid#')" title="选中" >选中</a>
</script>
<script type="text/html" id="tableToolBar">
<a class="tableToolBarBtn" href="javascript:remove('#yljgid#')" title="移除" >移除</a>
</script>
<script type="text/html" id="kslb">
<input  class="ks" index="#index#"/>
</script>
<script type="text/html" id="jg">
<div region="center" style="border: none;height: 330px">
	<table id="yljgForm">
	</table> 
</div>
</script>
<script type="text/html" id="ks">
<div region="west" style="width:64%;height: 330px;float:left;">
	<table id="yljgForm">
	</table>
</div>
<div region="east" style="width:35%;height: 330px;float:left;">
	<table id="xzjgTable">
	</table>
</div>	
</script>
</html>



